1 - Introduction

image-20251018082149687

image-20251018082255856

image-20251018082336016

2 - Expo vs React Native

不是学习react native吗?怎么出现了一个expo又是什么?

因为从零建立一个react native项目很复杂,所以出现了expo框架来帮助我们,就像react里面的nextjs一样,都是为了更好的做项目。

现在react native官方推荐使用expo这个框架来编写,所以直接学习这个expo框架。

image-20251018082903249

image-20251018083550289

官方推荐:

image-20251018083647834

3 - Hello World

参考文档:https://docs.expo.dev/tutorial/create-your-first-app/

创建项目npx create-expo-app@latest --template HelloWorld,再运行npm run reset-project,输入n,先来看一下项目结构。

reset-project script resets the app directory structure in a project and copies the previous boilerplate files from the project's root directory to another sub-directory called app-example. We can delete it since it is not part of our main app's structure.

重要的是app.json文件,里面控制项目在开发、构建、提交和更新应用期间的行为。

app文件夹里面的index.ts,就是项目的首页。

4 - Running App on Device

npx expo start运行项目,在手机上安装expo go应用,使用ios自带的二维码相机扫描,就可以在手机上打开项目了。而且代码的修改能够实时在手机上查看,这一点太棒了。

如果手机上没有及时更新,可能就是之前项目报错了或者是手机切换到别的应用了,此时需要在命令行里面输入r,来reload app,这样就可以实时更新了。

5 - Running App on iPhone Simulator

这个只能运行在macos上面,先不管。

6 - Running App on Android Emulator

这个要安装android studio,还是不安装了。

7 - Core Components

在react中,我们使用HTML tags来编写。

image-20251019133702985

都是react native里面没有dom的概念,因为app不是运行在浏览器环境的,而是运行在ios或者android环境的。

在ios或者android中,使用view标签来表示块。view是一个小的长方形元素,可以展示文本、图片、对用户输入做出响应。

image-20251019133850182

下面是一些react native提供的UI组件。

image-20251019134425372

在react native中使用这些组件,需要先引入,然后才能使用。在react中使用div、img这些标签,就不需要先引入。这是不同的地方。

image-20251019134845244

8 - View

这一节讲解View组件,这个组件是react native 的核心组件之一,可以作为容器组件,相当于web里面的div标签。View组件可以嵌套在View组件中。

image-20251019134738758

老师是创建了一个新的项目CoreComponents,但是我还是使用HelloWorld这个项目来学习。删除app/index.tsx里面的代码,自己写一些代码试一下效果。

手机上的效果:

image-20251019141835042

9 - Text

Text组件就是为了展示文本,可以嵌套使用,可以编写样式,可以处理touch事件。

image-20251019140757116

在app/index.tsx里面,直接在View里面写文本,看一下效果:

 

image-20251019141852866

image-20251019141909658

需要注意的是,web端是显示正常的,但是在手机上是会报错的,说明写的react native项目还真的需要模拟器或者手机来测试,使用web来测试有漏洞。

报错信息提示的很清楚了,在react native中,所有的文本节点都必须包裹在文本组件内,所以改成这样看一下:

可以看到,正常显示了:

image-20251019143201361

Text组件还可以嵌套自己,比如说将Hello改为白色,就这样做:

image-20251019143207985

10 - Image,ImageBackground

Image

Image组件可以展示静态图片、网络图片、本地图片。使用source属性来指定图片的地址。

image-20251019143330937

可以使用picsum.photos里面提供的网络图片地址,来当作network images。

image-20251019145644466

ImageBackground

ImageBackground组件,用来创建背景图片。

image-20251019145652625

11 - Scroll View

为了说明ScrollView的作用,编写如下代码,查看效果:

手机上查看效果,文字没有显示完整,并且不能滑动页面来查看第二张图片:

image-20251019151442172

这是web开发和react native开发的一个关键区别,于是react native提供了ScrollView组件来解决滚动的问题。

image-20251019150611981

将最外层的View组件改为ScrollView看一下效果:

可以看到最底下是没有padding的,可是我明明设置了padding啊,这是因为ScrollView上的padding不会像CSS那样运作(其实说不定就是ScrollView的一个bug),解决办法就是在ScrollView上面加一层View,在View上面定义padding。

 

12 - Button

Button组件在ios和android上的表现不同,需要特别指定。

image-20251019160507941

title属性

与web里面的Button组件相比,react native里面的Button组件是一个自闭合的标签,意味着不能以children的方式设置它的标题,而是需要通过它的title属性来指定。

可以看到,在IOS上Button是一个文字样式的,在android上是一个button样式的,我们可以设置样式来解决这个问题,但现在先不用管。

image-20251019160921513

onPress属性

类似于web端的button,它有一个onClick事件。那么在react native里面的button,它的事件是onPress,不要搞混了。

在手机上点击按钮,可以看到terminal里面的输出:

image-20251019161332661

color属性

color属性用来设置button的颜色,ios和android表现不同。

image-20251019161836732

disabled属性

disabled属性用来设置按钮是否可以点击。

image-20251019161843698

13 - Pressable

有时候我们需要在image或Text标签上触发点击事件,这时候就可以使用Pressable组件包裹住Image或者Text组件,点击Image或者文本就可以触发事件了。

Pressable使用onPress属性,来处理press事件。

image-20251019162121050

在手机上按Image、Text会触发相应的事件。

image-20251019163624672

那么就可以使用Pressable来创建一个自定义的button,这样就可以解决Button组件在ios和android上显示不同的问题了。

Pressable还提供多个事件属性,可以用来监听不同的操作。

image-20251019163151087

下面是图示:

image-20251019163244876

课后习题:设置各种事件,看一下触发的时机。

可以看到,longpressed触发时,是不会触发press事件的。

image-20251019164055814

14 - Modal

Modal组件会显示在app内容的上面,提供重要信息或者给与用户决策。

image-20251019164256061

visible属性

Modal的visible属性用来设置Modal是否可见,onRequestClose是设置用户在点击返回按钮时触发的事件(在android是通过底部的返回键来触发的,在ios里面是通过home键或者从bottom向上滑动来触发的)。

animationType属性

这个属性控制modal的动画效果。

看上面Modal出现和消失,感觉有点生硬,这是因为Modal的animationType默认是none,它还有两种值fade和slide,分别看一下效果:

fade

slide

 

presentationStyle属性

这个属性控制modal怎么展示。有四种值,默认是fullScreen。

TypeDefault
enum('fullScreen', 'pageSheet', 'formSheet', 'overFullScreen')fullScreen if transparent={false}``overFullScreen if transparent={true}

 

看一下pageSheet的效果:

15 - Status Bar

这个组件我还是第一次遇到,之前写小程序的时候,是不能够控制手机顶部栏的,所以这一点我是很难理解的,官提供了一个案例,可以在手机上体验一下https://reactnative.dev/docs/statusbar

image-20251019184659845

可以看到,真的是控制手机顶部栏,之前真的没有操作过这个东西,还是蛮新奇的。

image-20251019184311367

Status Bar指的就是红框内的部分。

image-20251019184257369

上面的代码是我根据学习的内容写的,还是比较简单的,就是来切换barStyle查看效果:

老师讲解了三个属性,当然还有很多。backgroundColor属性只在android端起作用,在ios中没有作用。barStyle属性有三种值,设置文字的颜色Sets the color of the status bar text。hidden属性,设置是否隐藏Status bar。

16 - Activity Indicator

就类似于web端的loading。

image-20251019201830020

size属性

用来设置activity indicator的大小,有两种值。

Size of the indicator.

TypeDefault
enum('small', 'large')number Android'small'

color属性

设置spinner的前景色。

The foreground color of the spinner.

TypeDefault
colornull (system accent default color)Android,'#999999' iOS

animating属性

设置显示或者隐藏activity indicator。

Whether to show the indicator (true) or hide it (false).

TypeDefault
booltrue

17 - Alert

Alert严格意义上来说是一个API,但是它可以调起一个alert dialog,所以老师把它作为core components的一部分来讲解了。

image-20251019202821052

Alert的alert方法类型:

Parameters:

NameTypeDescription
title RequiredstringThe dialog's title. Passing null or empty string will hide the title.
messagestringAn optional message that appears below the dialog's title.
buttonsAlertButton[]An optional array containing buttons configuration.
optionsAlertOptionsAn optional Alert configuration.

 

image-20251019203927445

18 - Custom Components

image-20251019204033350

上面我们学习了10个core components,很多情况下我们通过组合这些组件,来编写自定义组件。

创建组件HelloWorld\components\Greet.tsx,自定义一个简单组件。

引入到app/index.tsx中,查看效果:

image-20251019211429936

到这里就学完了react native里面的core components,还有很多components和API,这些都可以在做项目的时候学习。

接下来学习styling。